package com.baicai.baicaiknowledgeai.controller;

import com.aliyun.bailian20231229.models.RetrieveResponse;
import com.baicai.baicaiknowledgeai.entity.User;
import com.baicai.baicaiknowledgeai.service.IKnowDiyManageService;
import com.baicai.baicaiknowledgeai.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    private static final Logger logger = LoggerFactory.getLogger(ChatController.class);

    @Autowired
    private IKnowDiyManageService knowDiyManageService;

    @Autowired
    private IUserService userService;

    /**
     * 检索知识库内容
     */
    @GetMapping
    public RetrieveResponse chat(@RequestParam String query,
                                 @RequestParam String username) throws Exception {
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        return knowDiyManageService.retrieveKnowledgeContent(user, query);
    }
}